package com.briup.day25;

import java.io.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;

public class Test_Properties {
    public static void main(String[] args) throws Exception {
        //1.创建一个输入流对象  关联文件
        InputStream is = new FileInputStream("01-Basic/src/dir/db.Properties");
        //2.创建properties工具类对象
        Properties prop = new Properties();
        //3.load方法载入输入流对象
        prop.load(is);
        String url = prop.getProperty("url");
        String username = prop.getProperty("username");
        String password = prop.getProperty("password");
        System.out.println("url:"+url);
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        Enumeration<?> enumeration = prop.propertyNames(); // 获取枚举器
        while (enumeration.hasMoreElements()) { // 第1次判断：循环条件检查
            Object o = enumeration.nextElement(); // 获取下一个元素
            System.out.println("key:"+o);
            System.out.println("value:"+prop.getProperty(o.toString()));
        } // 循环回到 while 条件判断（第2次、第3次...）

        Set<String> keys = prop.stringPropertyNames();
        System.out.println(keys);
        for(String k:keys){
            System.out.println(k+" : "+prop.getProperty(k));
        }
        is.close();
        System.out.println("_-------------------------_");
        //修改配置文件中的属性
        prop.setProperty("username","root");
        prop.setProperty("password","root");
        //将修改的
        OutputStream os = new FileOutputStream("01-Basic/src/dir/db.Properties");
        prop.store(os,"this is a test");//将修改提交到
    }
}
